package com.myandroid.calllogviewer;

import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.CallLog;
import android.view.View;

import android.widget.SimpleCursorAdapter;
import android.widget.SimpleCursorAdapter.ViewBinder;
import android.widget.TextView;

public class CallLogViewerActivity extends ListActivity {
    /** Called when the activity is first created. */
	
	private SimpleCursorAdapter		adapter;
	private Cursor					c;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        c	= managedQuery(CallLog.Calls.CONTENT_URI, null, null, null, null);
        
        final String[] from	= new String[] { CallLog.Calls.NUMBER, CallLog.Calls.TYPE };
        int[] to			= new int[] { android.R.id.text1, android.R.id.text2 };

        adapter				= new SimpleCursorAdapter(this, 
        								android.R.layout.simple_list_item_2, c, from, to);
        
        adapter.setViewBinder(new ViewBinder() {
        	@Override
        	public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
        		switch(view.getId()) {
        			case android.R.id.text2 :
        				TextView tv	= (TextView)view;
        				int value	= cursor.getInt(cursor.getColumnIndex(CallLog.Calls.TYPE));
        				
        				switch(value) {
        					case CallLog.Calls.INCOMING_TYPE :
        						tv.setText("수신");
        						break;
        					case CallLog.Calls.MISSED_TYPE :
        						tv.setText("부재중 통화");
        						break;
        					case CallLog.Calls.OUTGOING_TYPE :
        						tv.setText("발신");
        						break;
        				}        				
        				return true;
        		}        		
        		return false;
        	}
        	
        });
        setListAdapter(adapter);
    }
}